home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Technology Seed / ADC Seed CD - July 1999.toast / Carbon SDK 1.0d10c3 / Sample Code / SimpleText / MovieFile.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-04  |  8.9 KB  |  317 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        MovieFile.c
  3.  
  4.     Contains:    Movie file support for simple text application
  5.  
  6.     Version:    SimpleText 1.4 or later
  7.  
  8.     Written by:    Tom Dowdy
  9.  
  10.     Copyright:    © 1993, 1995-1998 by Apple Computer, Inc., all rights reserved.
  11.  
  12.     File Ownership:
  13.  
  14.         DRI:                Tom Dowdy
  15.  
  16.         Other Contact:        Jim Negrette
  17.  
  18.         Technology:            Macintosh Graphics Group
  19.  
  20.     Writers:
  21.  
  22.         (ecs)    Eric Schlegel
  23.         (dmp)    Dave Polaschek
  24.         (ted)    Tom Dowdy
  25.         (TD)    Tom Dowdy
  26.  
  27.     Change History (most recent first):
  28.  
  29.     $Log: MovieFile.c,v $
  30.     Revision 1.3  1999/05/04 20:03:34  jiarocci
  31.     Fix for #2331923 - Expletive in SimpleText sample code.
  32.     
  33.     Revision 1.2  1998/11/19 01:16:16  wilkes
  34.     Removed reliance on QuickTime.  Most changes were conditionalized with
  35.     ALLOW_QUICKTIME...
  36.  
  37.     Revision 1.1.1.1  1998/03/18 22:56:10  ivory
  38.     Initial checkin of SimpleText.
  39.  
  40.         
  41.         4     3/6/98 11:35 AM Tom Dowdy
  42.         If NewMovieController fails (ie, NIL controller) but doesn't set the
  43.         movie error, we'll assume memFullErr.
  44.         
  45.         3     11/11/97 11:08 PM Sam Bushell
  46.         Check for NewMovieController failing.
  47.         
  48.         2     7/29/97 2:06 PM Tom Dowdy
  49.         Removed all of the old and boring refs
  50.         
  51.         1     7/28/97 11:19 AM Duane Byram
  52.         first added to Source Safe project
  53.  
  54.          <7>     4/28/97    ted        [1650434]  Crash w/ QuickTime -- also fix incompat w/ AppleGuide
  55.          <6>     12/4/96    ted        [1605686]  Low mem check after open.
  56.          <5>    11/26/96    ecs        smarter cursor adjustment
  57.          <4>      9/9/96    dmp        staticfy local functions to eliminate warnings in MWC.
  58.          <3>     8/28/96    ted        adding find
  59.          <2>     10/2/95    TD        adding support for SC compiler
  60.          <1>     8/21/95    TD        First checked in.
  61.  
  62. */
  63.  
  64. #include "MacIncludes.h"
  65.  
  66. #include "MovieFile.h"
  67.  
  68. #pragma segment MovieFile
  69.  
  70. #if ALLOW_QUICKTIME
  71. // --------------------------------------------------------------------------------------------------------------
  72. static void DoFindInMovie(Movie theMovie, long *searchOffset, Boolean goBackwards)
  73. {
  74.     Track    searchTrack = nil;
  75.     OSErr    anErr;
  76.     
  77.     if (goBackwards)
  78.         (*searchOffset)--;
  79.     else
  80.         (*searchOffset)++;
  81.  
  82.     anErr = MovieSearchText(theMovie, (Ptr)&gFindString[1], gFindString[0],
  83.                 (gWrapAround ? findTextWrapAround : 0) |
  84.                 (gCaseSensitive ? findTextCaseSensitive : 0) |
  85.                 findTextUseOffset |
  86.                 (goBackwards ? findTextReverseSearch : 0),
  87.                 &searchTrack, (TimeValue*)nil, searchOffset);
  88.                 
  89.     if (anErr)
  90.         SysBeep(1);
  91. } // DoFindInMovie
  92.  
  93. // --------------------------------------------------------------------------------------------------------------
  94.  
  95. static OSErr    MovieAdjustMenus(WindowPtr pWindow, WindowDataPtr pData)
  96. {
  97. #pragma unused (pWindow)
  98.  
  99.     if (GetMovieIndTrackType( ((MovieDataPtr)pData)->theMovie, 1, 'text', movieTrackCharacteristic) != nil)
  100.         {
  101.         EnableCommand(cFind);
  102.         if (gFindString[0] != 0)
  103.             EnableCommand(cFindAgain);
  104.         }
  105.  
  106.     return noErr;
  107.     
  108. } // MovieAdjustMenus
  109.  
  110. // --------------------------------------------------------------------------------------------------------------
  111.  
  112. static OSErr    MovieCommand(WindowPtr pWindow, WindowDataPtr pData, short commandID, long menuResult)
  113. {
  114. #pragma unused (pWindow, menuResult)
  115.  
  116.     OSErr    anErr = noErr;
  117.     
  118.     switch (commandID)
  119.         {
  120.         case cFind:
  121.             if (ConductFindOrReplaceDialog(kFindWindowID) == cancel)
  122.                 break;
  123.         
  124.         case cFindAgain:
  125.             DoFindInMovie(((MovieDataPtr)pData)->theMovie, 
  126.                         &((MovieDataPtr)pData)->searchOffset,
  127.                         ((gEvent.modifiers & shiftKey) != 0));
  128.             break;
  129.         }
  130.     
  131.     return(anErr);
  132.     
  133. } // MovieCommand
  134.  
  135. // --------------------------------------------------------------------------------------------------------------
  136.  
  137. static OSErr    MovieCloseWindow(WindowPtr pWindow, WindowDataPtr pData)
  138. {
  139. #pragma unused (pWindow)
  140.  
  141.     DisposeMovieController( ((MovieDataPtr)pData)->thePlayer);
  142.     DisposeMovie( ((MovieDataPtr)pData)->theMovie);
  143.     CloseMovieFile(pData->resRefNum);
  144.     pData->resRefNum = -1;
  145.     
  146.     return(noErr);
  147.     
  148. } // MovieCloseWindow
  149.  
  150. // --------------------------------------------------------------------------------------------------------------
  151. static OSErr MovieAdjustCursor(WindowPtr pWindow, WindowDataPtr pData, Point *localMouse, RgnHandle globalRgn)
  152. {
  153. #pragma unused (pWindow, pData, globalRgn, localMouse)
  154.     
  155.     return(eActionAlreadyHandled);
  156.     
  157. } // MovieAdjustCursor
  158.  
  159. // --------------------------------------------------------------------------------------------------------------
  160.  
  161. static OSErr    MovieGetBalloon(WindowPtr pWindow, WindowDataPtr pData, 
  162.         Point *localMouse, short * returnedBalloonIndex, Rect *returnedRectangle)
  163. {
  164. #pragma unused (pWindow, pData, localMouse, returnedRectangle)
  165.  
  166.     *returnedBalloonIndex = iDidTheBalloon;
  167.     
  168.     return(noErr);
  169.     
  170. } // MovieGetBalloon
  171.  
  172. // --------------------------------------------------------------------------------------------------------------
  173.  
  174. static Boolean    MovieFilterEvent(WindowPtr pWindow, WindowDataPtr pData, EventRecord *pEvent)
  175. {
  176. #pragma unused (pWindow)
  177.     
  178.     return(MCIsPlayerEvent( ((MovieDataPtr)pData)->thePlayer, pEvent));
  179. } // MovieFilterEvent
  180.  
  181. // --------------------------------------------------------------------------------------------------------------
  182.  
  183. static long MovieCalculateIdleTime(WindowPtr pWindow, WindowDataPtr pData)
  184. {
  185. #pragma unused (pWindow, pData)
  186.  
  187.     if (!IsMovieDone( ((MovieDataPtr)pData)->theMovie))
  188.         return(0);
  189.     else
  190.         return(0x7FFFFFFF);
  191.         
  192. } // MovieCalculateIdleTime
  193.  
  194. // --------------------------------------------------------------------------------------------------------------
  195.  
  196. static OSErr    MovieMakeWindow(WindowPtr pWindow, WindowDataPtr pData)
  197. {
  198.     OSErr                anErr;
  199.     short                actualResId;
  200.     Movie                theMovie;
  201.     MovieController        thePlayer;
  202.     Rect                movieBounds;
  203.     long                version;
  204.     
  205.     Gestalt(gestaltQuickTime, &version);
  206.     
  207.     pData->pAdjustMenus            = (AdjustMenusProc)            MovieAdjustMenus;
  208.     pData->pCommand                = (CommandProc)                MovieCommand;
  209.     pData->pCloseWindow         = (CloseWindowProc)            MovieCloseWindow;
  210.     pData->pFilterEvent         = (FilterEventProc)            MovieFilterEvent;
  211.     pData->pGetBalloon             = (GetBalloonProc)            MovieGetBalloon;
  212.     pData->pCalculateIdleTime    = (CalculateIdleTimeProc)    MovieCalculateIdleTime;
  213.     pData->pAdjustCursor        = (AdjustCursorProc)        MovieAdjustCursor;
  214.     pData->dragWindowAligned    = (version >= 0x01508000);
  215.     
  216.     actualResId = DoTheRightThing;
  217.  
  218.     // close down other paths to the file -- because we don't need them
  219.     if (pData->resRefNum != -1)
  220.         {
  221.         CloseResFile(pData->resRefNum);
  222.         pData->resRefNum = -1;
  223.         }
  224.     if (pData->dataRefNum != -1)
  225.         {
  226.         FSClose(pData->dataRefNum);
  227.         pData->dataRefNum = -1;
  228.         }
  229.  
  230.     anErr = OpenMovieFile(&pData->fileSpec, &pData->resRefNum, 0);
  231.     if (anErr == noErr)
  232.         {
  233.         anErr = NewMovieFromFile(&theMovie, pData->resRefNum, &actualResId, (unsigned char *) 0, newMovieActive, (Boolean *) 0);
  234.         if (anErr == noErr)
  235.             anErr = GetMoviesError();
  236.         
  237.         // leave slop because some movies/VR don't work with low mem, but eat it anyway
  238.         if (FreeMem() < kRAMNeededForNew)
  239.             {
  240.             DisposeMovie(theMovie);
  241.             anErr = memFullErr;
  242.             }
  243.         }
  244.     else
  245.         {
  246.         pData->resRefNum = -1;
  247.         }
  248.     nrequire(anErr, NewMovieFromFile);
  249.     
  250.     // position the movie
  251.     GetMovieBox(theMovie, &movieBounds);
  252.     OffsetRect(&movieBounds, -movieBounds.left, -movieBounds.top);
  253.     SetMovieBox(theMovie, &movieBounds);
  254.     OffsetRect(&movieBounds, pData->contentRect.left, pData->contentRect.top);
  255.  
  256.     // make it draw in the correct window
  257.     SetMovieGWorld(theMovie, (CGrafPtr) pWindow, 0);
  258.     thePlayer = NewMovieController(theMovie, &movieBounds, mcTopLeftMovie);
  259.     anErr = GetMoviesError();
  260.     if ((anErr == noErr) && (!thePlayer)) anErr = memFullErr;
  261.     nrequire(anErr, NewMovieFromFile);
  262.     MCGetControllerBoundsRect(thePlayer, &movieBounds);
  263.  
  264.     // make sure the window is the proper size
  265.     SizeWindow(pWindow, movieBounds.right - movieBounds.left,
  266.                             movieBounds.bottom - movieBounds.top, false);
  267.     pData->contentRect.right = pData->contentRect.left + 
  268.         movieBounds.right - movieBounds.left;
  269.     pData->contentRect.bottom = pData->contentRect.top + 
  270.         movieBounds.bottom - movieBounds.top;
  271.     if (pData->dragWindowAligned)
  272.         AlignWindow((WindowPtr)pWindow, false, nil, nil);
  273.     
  274.     // enable keyboard events
  275.     MCDoAction(thePlayer, mcActionSetKeysEnabled, (void*)1);
  276.     
  277.     // Save a reference to the movie
  278.     ((MovieDataPtr)pData)->theMovie = theMovie;
  279.     ((MovieDataPtr)pData)->thePlayer = thePlayer;
  280.     ((MovieDataPtr)pData)->searchOffset = 0;
  281.     
  282. // FALL THROUGH EXCEPTION HANDLING
  283. NewMovieFromFile:
  284.     return(anErr);
  285. } // MovieMakeWindow
  286.  
  287. #endif
  288.  
  289. // --------------------------------------------------------------------------------------------------------------
  290.  
  291. OSErr    MoviePreflightWindow(PreflightPtr pPreflightData)
  292. {    
  293. #if ALLOW_QUICKTIME
  294.     pPreflightData->continueWithOpen     = true;
  295.     pPreflightData->makeProcPtr         = MovieMakeWindow;
  296.     pPreflightData->resourceID            = kMovieWindowID;
  297.     pPreflightData->storageSize         = sizeof(MovieDataRecord);
  298. #endif
  299.  
  300.     return(noErr);
  301.     
  302. } // MoviePreflightWindow
  303.  
  304. // --------------------------------------------------------------------------------------------------------------
  305.  
  306. void MovieGetFileTypes(OSType * pFileTypes, OSType * pDocumentTypes, short * numTypes)
  307. {
  308. #if ALLOW_QUICKTIME
  309.     if (gMachineInfo.haveQuickTime)
  310.         {
  311.         pFileTypes[*numTypes]         = 'MooV';
  312.         pDocumentTypes[*numTypes]     = kMovieWindow;
  313.         (*numTypes)++;
  314.         }
  315. #endif
  316. } // MovieGetFileTypes
  317.